import React, { useState } from 'react';
import { 
  Container, 
  Typography, 
  Box, 
  Tabs, 
  Tab, 
  Paper,
  Divider
} from '@mui/material';
import ResourceManagement from '../components/ResourceManagement';
import PromotionManagement from '../components/PromotionManagement';
import { useAuth } from '../../auth/AuthContext';
import { Navigate } from 'react-router-dom';

interface TabPanelProps {
  children?: React.ReactNode;
  index: number;
  value: number;
}

function TabPanel(props: TabPanelProps) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`admin-tabpanel-${index}`}
      aria-labelledby={`admin-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box sx={{ p: 3 }}>
          {children}
        </Box>
      )}
    </div>
  );
}

function a11yProps(index: number) {
  return {
    id: `admin-tab-${index}`,
    'aria-controls': `admin-tabpanel-${index}`,
  };
}

const AdminPage: React.FC = () => {
  const [tabValue, setTabValue] = useState(0);
  const { user } = useAuth();

  // 移除管理员权限检查，让所有用户都能访问
  // if (!user || user.role !== 'admin') {
  //   return <Navigate to="/" replace />;
  // }

  const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
    setTabValue(newValue);
  };

  return (
    <Container maxWidth="lg" sx={{ py: 4 }}>
      <Typography variant="h4" gutterBottom>
        管理控制台
      </Typography>
      <Divider sx={{ mb: 3 }} />
      
      <Paper sx={{ width: '100%' }}>
        <Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
          <Tabs 
            value={tabValue} 
            onChange={handleTabChange} 
            aria-label="管理选项卡"
            variant="fullWidth"
          >
            <Tab label="资源管理" {...a11yProps(0)} />
            <Tab label="促销活动管理" {...a11yProps(1)} />
          </Tabs>
        </Box>
        
        <TabPanel value={tabValue} index={0}>
          <ResourceManagement />
        </TabPanel>
        
        <TabPanel value={tabValue} index={1}>
          <PromotionManagement />
        </TabPanel>
      </Paper>
    </Container>
  );
};

export default AdminPage;